package com.xpsoft.framework.aop;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import com.opensymphony.xwork2.ActionContext;
import com.xpsoft.framework.util.PropertiesUtil;

public class LogAdvice {
	private static Log log = LogFactory.getLog(LogAdvice.class);

	public void logBefore(final JoinPoint joinPoint) {
//		  // 获取方法名
//	       System.out.println("-----方法:" + joinPoint.getSignature().getName());
//	       // 获取参数信息
//	       Object[] args = joinPoint.getArgs();
//	       for (int i = 0; i < args.length; i++) {
//	           System.out.println("-------参数" + args[i]);
//	       }
	}

   public Object around(ProceedingJoinPoint p) throws Throwable{
//       System.out.println("-----------comeinto around");
	   long stime = System.nanoTime();
       //返回值
       Object retVal = p.proceed();  
       // 获取方法名
       log.info("类名:"+p.getTarget().getClass().getName()+"-----方法:" + p.getSignature().getName()+"-----执行时间:"+(System.nanoTime()-stime)/1000000+"毫秒");
       // 获取参数信息
       Object[] args = p.getArgs();
       //调用方法参数
//       for (int i = 0; i < args.length; i++) {
//           System.out.println("-------参数" + args[i]);
//       }
       
    //得到struts1 从拦截的方法中参数中得到四个对象 
//       ActionMapping mapping = (ActionMapping) p.getArgs()[0]; 
//       ActionForm form = (ActionForm) p.getArgs()[1]; 
//       HttpServletRequest request = (HttpServletRequest) p.getArgs()[2]; 
//       HttpServletResponse response = (HttpServletResponse) p.getArgs()[3]; 


       //得到struts2 的相关 request等对象
//       Map<String,Object> session =  (Map<String,Object>)ActionContext.getContext().getSession();
       HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
//       HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);  
       //获取request所有请求参数
//       Enumeration params = request.getParameterNames();
//		 while (params.hasMoreElements()) { 
//           String param = (String) params.nextElement();
//           String value = request.getParameter(param);
//           System.out.println("param=" + param+","+"value=" + value);
//		 }
//       System.out.println("-----------leave around");
       return retVal;
    }
	
	public void logAfter(final JoinPoint joinPoint) {
		logMsg(joinPoint);
	}

	public void logExitAfterReturn(final JoinPoint joinPoint) {
		logMsg(joinPoint);
	}

	public void logAfterThrow(final JoinPoint joinPoint) {
		logMsg(joinPoint);
	}

	private void logMsg(final JoinPoint joinPoint) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < joinPoint.getArgs().length; i++) {
			if (i < joinPoint.getArgs().length - 1) {
				sb.append(joinPoint.getArgs()[i]).append(",");
			} else {
				sb.append(joinPoint.getArgs()[i]);
			}
		}

		log.debug(" Entering " + joinPoint.getTarget().getClass().getName()
				+ "." + joinPoint.getSignature().getName() + "("
				+ sb.toString() + ")");
	}
}
